package com.example.chongwushangcheng.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.petshopping.R;

import java.util.List;

public class CarouselAdapter extends RecyclerView.Adapter<CarouselAdapter.CarouselViewHolder> {

    private Context context;
    private List<String> imageUrls;
    private List<Integer> imageResources;
    private boolean useDrawableResources = false;

    public CarouselAdapter(Context context, List<String> imageUrls) {
        this.context = context;
        this.imageUrls = imageUrls;
        this.useDrawableResources = false;
    }

    // 重载构造函数支持drawable资源ID
    public CarouselAdapter(Context context, List<Integer> imageResources, boolean useDrawableResources) {
        this.context = context;
        this.imageResources = imageResources;
        this.useDrawableResources = useDrawableResources;
    }

    @NonNull
    @Override
    public CarouselViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_carousel_image, parent, false);
        return new CarouselViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CarouselViewHolder holder, int position) {
        if (useDrawableResources) {
            // 使用drawable资源ID
            int resourceId = imageResources.get(position);
            holder.imageView.setImageResource(resourceId);
        } else {
            // 使用图片URL，加载网络图片
            String imageUrl = imageUrls.get(position);
            Glide.with(context)
                    .load(imageUrl)
                    .placeholder(R.drawable.ic_user_avatar)
                    .error(R.drawable.ic_user_avatar)
                    .into(holder.imageView);
        }
    }

    @Override
    public int getItemCount() {
        if (useDrawableResources) {
            return imageResources != null ? imageResources.size() : 0;
        } else {
            return imageUrls != null ? imageUrls.size() : 0;
        }
    }

    static class CarouselViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public CarouselViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.ivCarousel);
        }
    }
} 